package com.sbr.tbd.wicket.pages.tbd;

import com.sbr.tbd.dao.FeatureDao;
import com.sbr.tbd.model.Feature;
import com.sbr.tbd.wicket.pages.BasePage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.Iterator;
import java.util.List;

public class TBDPage extends BasePage {

    @SpringBean
    private FeatureDao featureDao;

    public TBDPage() {
        super();

        //ListView
//		add(new ListView<Feature>("features", featureDao.findAll()) {
//
//			@Override
//			protected void populateItem(ListItem<Feature> item) {
//				Feature feature = item.getModelObject();
//				item.add(new Label("name", feature.getName()));
//			}
//
//		});


        IDataProvider<Feature> dataProvider = new IDataProvider<Feature>() {

            private List<Feature> features = featureDao.readAll();

            @Override
            public void detach() {
                // TODO Auto-generated method stub

            }

            @Override
            public Iterator<Feature> iterator(int first, int count) {
                return features.subList(first, first + count).iterator();
            }

            @Override
            public IModel<Feature> model(Feature feature) {
                return new Model<Feature>(feature);
            }

            @Override
            public int size() {
                return features.size();
            }

        };



        //  GridView
        GridView<Feature> gridView = new GridView<Feature>("features", dataProvider ) {
            @Override
            protected void populateItem(Item<Feature> item) {
                final Feature feature = item.getModelObject();
                item.add(new Label("name", feature.getName()));
            }

            @Override
            protected void populateEmptyItem(Item<Feature> item) {
                item.add(new Label("name", "*empty*"));
            }
        };

        gridView.setRows(4);
        gridView.setColumns(3);

        add(gridView);


        /*
        DataGridView
        List<ICellPopulator<Contact>> columns = new ArrayList<ICellPopulator<Contact>>();

        columns.add(new PropertyPopulator<Contact>("id"));
        columns.add(new PropertyPopulator<Contact>("firstName"));
        columns.add(new PropertyPopulator<Contact>("lastName"));
        columns.add(new PropertyPopulator<Contact>("homePhone"));
        columns.add(new PropertyPopulator<Contact>("cellPhone"));

        add(new DataGridView<Contact>("rows", columns, new SortableContactDataProvider()));

        */


    }

    public void setFeatureDao(FeatureDao featureDao) {
        this.featureDao = featureDao;
    }
}
